// var margin = 5;//图片间距
var api = require('../../utils/api.js');
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow: true,
    classindex: 0,
    priceindex: 0,
    priceArr: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    grademark: '',
    apptoken: '',
    average: '',
    backdrop: [],
    group: [],
    notice: [],
    temImagePath: '',
    bid: ''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    // wx.getSystemInfo({
    //   success: function (res) {
    //   }
    // });
    this.setingInfoRequest();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //设置信息请求
  setingInfoRequest() {
    var that = this;
    try {
      var value = wx.getStorageSync('token')
      if (value) {
        that.setData({
          apptoken : value
        })
      }
    } catch (e) {
    }
    api.post({
      url: 'Live/initial',
      data: { token: that.data.apptoken },
      success: data => {
        if (data.code == 200) {
          var show = true;
          if (data.data.backdrop.bid){
            show = true;
          } else {
            show = false;
          }
          that.setData({
            backdrop: data.data.backdrop,
            group: data.data.group,
            notice: data.data.notice,
            average: data.data.average,
            isShow: show,
            temImagePath: data.data.backdrop.image,
            bid: data.data.backdrop.bid
          })
        }
      }
    })
  },

  //选择器
  bindPickerClassChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      classindex: e.detail.value,
    })
    var that = this;
    that.data.liveGid = that.data.group[that.data.classindex].gid;
  },
  bindPickerPriceChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      priceindex: e.detail.value
    })
    var that = this;
    that.data.livePrice = that.data.priceArr[that.data.priceindex]
  },
  //直播标题
  onLiveTitleInput(e) {
    this.setData({
      liveTitle : e.detail.value
    })
  },

  //选择图片
  ChonicePhoto: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认1
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var temps;
        temps = res.tempFilePaths;
        that.setData({
          temImagePath: temps[0],
          isShow: true
        });
        wx.showLoading({
          title:'上传中...'
        });
        wx.uploadFile({
          url: 'https://cum.api.livestaring.com/v001/About/upload',
          filePath: that.data.temImagePath,
          name: 'file',
          formData: {
            'token': that.data.apptoken,
            'content-type': 'multipart/form-data'
          },
          success: function (res) {
            var severObj = JSON.parse(res.data)
            if (res.statusCode = 200) {
              if (severObj.code = 200) {
                wx.showToast({
                  title: '图片上传成功',
                  icon: 'none'
                })
               var addBkImg = that.addBackGroundImage(severObj.data.file, severObj.data.path);
              }
            }
          },
          fail: function (res) {
            wx.hideLoading();
            wx.showToast({
              title: '图片上传失败',
              icon: 'none'
            })
          },
          complete:function (res) {
            
          }
        })
      }
    })
  },
  //添加背景图片
  addBackGroundImage:function(file,path){
    var that = this;
    api.post({
      url:'Backdrop/add',
      data:{
        file:file,
        path:path,
        token:that.data.apptoken,
        status:2
      },
      success: data=>{
        if(data.code == 200){
          wx.showToast({
            title: data.data.message,
            icon: 'none'
          })
          that.setData({
            bid:data.data.bid
          })
        } else if (data.code == 400) {
          wx.showToast({
            title: data.data.message,
            icon: 'none'
          })
        }
      },
      complete: function (res) {
        wx.hideLoading();
      }
    })
  },
  //开启直播
  formSubmit: function (e) {
    var that = this;
    if (!that.data.liveTitle) {
      wx.showToast({
        title: '请输入直播标题!',
        icon: 'none'
      })
      return;
    }
    var index = e.detail.value.gid;
    e.detail.value.gid = that.data.group[index].gid;
    e.detail.value.token = that.data.apptoken;
    api.post({
      url: 'Live/start',
      data: e.detail.value,
      success: data => {
        if (data.code == 200) {
          wx.showToast({
            title: data.data.message,
            icon: 'none'
          });
          wx.setStorage({
            key: "livedata",
            data: data.data,
            success:function(){
              setTimeout(function () {
                wx.navigateTo({
                  url: '../liveroom/room/room?type=presenter&roomID='+data.data.lid+'&roomName='+data.data.name
                });
              }, 1000);
            }
          })

        } else if (data.code == 300) {
          wx.showToast({
            title: data.data.message,
            icon: 'none'
          })
        } else if (data.code == 400) {
          wx.showToast({
            title: data.data.message,
            icon:'none'
          })
        }
      }
    })
  }
})